package gui;


import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import messagesPacks.ActivityReportMessage;
import Entity.ActivityRecored;
import controllers.OBLSettings;
import javax.swing.border.LineBorder;
import java.awt.SystemColor;
import java.awt.Color;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import javax.swing.JRadioButton;
/**
 * This class representing a Manager activity report Gui
 * Date: 10/1/2013
 * @author Roman
 *
 */
public class ManagerActivityReportUI extends JPanel 
{
	private static final long serialVersionUID = 1L;
	public JButton btnLogout;
	public JButton btnExit;

	public SimpleTable SubTable;
	private JButton btnBack;
	private JLabel lblOblManager;
	private final JSeparator separator = new JSeparator();
	

	ManagerMainUI managerMainUI=null;
	private JLabel lblNumberOfActive;
	private JLabel lblNumberOfLocked;
	private JLabel lblNumberOfInactive;
	private JLabel lblNumberOfTotal;
	private JLabel lblNumberOfLateness;
	JTextField ActiveSubtextField;
	JTextField LockedSubtextField;
	JTextField InActiveSubtextField;
	JTextField TotalBorrowedtextField;
	JTextField LatenessSubtextField;
	private JLabel lblChooseReportDate;
	private JButton btnProduceReport;
    JComboBox<String> DatesComboBox;

    ActivityReportMessage activity_report;
    
    ActivityReportMessage activity_report_current;
    
    private JRadioButton rdbtnPercentage;
    private JRadioButton rdbtnInNumbers;
	
    String selectedDate;
    DecimalFormat df;
    
	public ManagerActivityReportUI(ManagerMainUI managerMainUI) 
	{
		super();
		setBackground(SystemColor.control);
		this.managerMainUI = managerMainUI;
		System.out.println("ManagerActivityReportUI created.");
		initialize();
	}
	private void initialize()
	{
		this.setSize(OBLSettings.pane2Size[0],OBLSettings.pane2Size[1]);
		setLayout(null);
	    df = new DecimalFormat("0.###");
    	separator.setBounds(0, 57, 855, 9);
		add(separator);
		lblOblManager = new JLabel("OBL Manager-Activity Report");
		lblOblManager.setFont(new Font("Tahoma", Font.BOLD, 20));
		lblOblManager.setBounds(245, 0, 310, 58);
		add(lblOblManager);
		
		btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnExit.setBounds(707, 11, 83, 41);
		add(btnExit);
		
		btnBack = new JButton("Back");
		btnBack.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnBack.setBounds(10, 11, 83, 41);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setFont(new Font("Tahoma", Font.BOLD, 15));
		btnLogout.setBounds(614, 11, 83, 41);
		add(btnLogout);
		
		lblNumberOfActive = new JLabel("Number of active subscribers");
		lblNumberOfActive.setHorizontalAlignment(SwingConstants.CENTER);
		lblNumberOfActive.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfActive.setBounds(-3, 155, 229, 41);
		add(lblNumberOfActive);
		
		lblNumberOfLocked = new JLabel("Number of locked subscribers");
		lblNumberOfLocked.setHorizontalAlignment(SwingConstants.CENTER);
		lblNumberOfLocked.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfLocked.setBounds(-2, 225, 229, 41);
		add(lblNumberOfLocked);
		
		lblNumberOfInactive = new JLabel("Number of suspended subscribers");
		lblNumberOfInactive.setHorizontalAlignment(SwingConstants.CENTER);
		lblNumberOfInactive.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfInactive.setBounds(11, 299, 229, 41);
		add(lblNumberOfInactive);
		
		lblNumberOfTotal = new JLabel("Number of total borrowed copies");
		lblNumberOfTotal.setHorizontalAlignment(SwingConstants.CENTER);
		lblNumberOfTotal.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfTotal.setBounds(0, 365, 240, 41);
		add(lblNumberOfTotal);
		
		lblNumberOfLateness = new JLabel("Number of lateness subscriber");
		lblNumberOfLateness.setHorizontalAlignment(SwingConstants.CENTER);
		lblNumberOfLateness.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblNumberOfLateness.setBounds(0, 435, 229, 41);
		add(lblNumberOfLateness);
		
		ActiveSubtextField = new JTextField();
		ActiveSubtextField.setFont(new Font("Tahoma", Font.BOLD, 20));
		ActiveSubtextField.setHorizontalAlignment(SwingConstants.CENTER);
		ActiveSubtextField.setEditable(false);
		ActiveSubtextField.setBorder(new LineBorder(new Color(0, 0, 0)));
		ActiveSubtextField.setBounds(245, 163, 188, 29);
		add(ActiveSubtextField);
		ActiveSubtextField.setColumns(10);
		
		LockedSubtextField = new JTextField();
		LockedSubtextField.setFont(new Font("Tahoma", Font.BOLD, 20));
		LockedSubtextField.setHorizontalAlignment(SwingConstants.CENTER);
		LockedSubtextField.setEditable(false);
		LockedSubtextField.setBorder(new LineBorder(new Color(0, 0, 0)));
		LockedSubtextField.setColumns(10);
		LockedSubtextField.setBounds(245, 233, 188, 29);
		add(LockedSubtextField);
		
		InActiveSubtextField = new JTextField();
		InActiveSubtextField.setFont(new Font("Tahoma", Font.BOLD, 20));
		InActiveSubtextField.setHorizontalAlignment(SwingConstants.CENTER);
		InActiveSubtextField.setEditable(false);
		InActiveSubtextField.setBorder(new LineBorder(new Color(0, 0, 0)));
		InActiveSubtextField.setColumns(10);
		InActiveSubtextField.setBounds(245, 303, 188, 29);
		add(InActiveSubtextField);
		
		TotalBorrowedtextField = new JTextField();
		TotalBorrowedtextField.setFont(new Font("Tahoma", Font.BOLD, 20));
		TotalBorrowedtextField.setHorizontalAlignment(SwingConstants.CENTER);
		TotalBorrowedtextField.setEditable(false);
		TotalBorrowedtextField.setBorder(new LineBorder(new Color(0, 0, 0)));
		TotalBorrowedtextField.setColumns(10);
		TotalBorrowedtextField.setBounds(245, 373, 188, 29);
		add(TotalBorrowedtextField);
		
		LatenessSubtextField = new JTextField();
		LatenessSubtextField.setFont(new Font("Tahoma", Font.BOLD, 20));
		LatenessSubtextField.setHorizontalAlignment(SwingConstants.CENTER);
		LatenessSubtextField.setEditable(false);
		LatenessSubtextField.setBorder(new LineBorder(new Color(0, 0, 0)));
		LatenessSubtextField.setColumns(10);
		LatenessSubtextField.setBounds(245, 443, 188, 29);
		add(LatenessSubtextField);
		
		DatesComboBox = new JComboBox<String>();
		DatesComboBox.setMaximumRowCount(25);
		DatesComboBox.setFont(new Font("Tahoma", Font.BOLD, 15));
		DatesComboBox.setBounds(202, 75, 395, 31);
		add(DatesComboBox);
		
		lblChooseReportDate = new JLabel("Choose Report date");
		lblChooseReportDate.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblChooseReportDate.setBounds(35, 77, 144, 24);
		add(lblChooseReportDate);
		
		btnProduceReport = new JButton("produce new report");

		btnProduceReport.setFont(new Font("Tahoma", Font.BOLD, 14));
		btnProduceReport.setBounds(614, 69, 176, 39);
		add(btnProduceReport);
		
		rdbtnInNumbers = new JRadioButton("in numbers");
		rdbtnInNumbers.setOpaque(false);
		rdbtnInNumbers.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rdbtnInNumbers.setBounds(202, 503, 109, 23);
		rdbtnInNumbers.setSelected(true);
		add(rdbtnInNumbers);
		
		rdbtnPercentage = new JRadioButton("in percentage");
		rdbtnPercentage.setOpaque(false);
		rdbtnPercentage.setFont(new Font("Tahoma", Font.PLAIN, 14));
		rdbtnPercentage.setBounds(35, 503, 131, 23);
		add(rdbtnPercentage);
		
		
		

//================================================================		
//                       Action Listeners
//================================================================     
	     
		     btnBack.addActionListener(new ActionListener() 
		     {
		     	public void actionPerformed(ActionEvent e) 
		     	{
					 ActiveSubtextField.setText("");
					 LockedSubtextField.setText("");
					 InActiveSubtextField.setText("");
					 TotalBorrowedtextField.setText("");
					 LatenessSubtextField.setText("");
			     	 rdbtnInNumbers.setSelected(true);
			     	 rdbtnPercentage.setSelected(false);
			     		
		     		 WelcomeUI.wui.switchPanel("OBL Manager",managerMainUI ,OBLSettings.pane2Size[0], OBLSettings.pane2Size[1]);

		     	}
		     });
//================================================================	
		     rdbtnInNumbers.addActionListener(new ActionListener()
		     {
		     	public void actionPerformed(ActionEvent arg0)
		     	{
		     		rdbtnInNumbers.setSelected(true);
		     		rdbtnPercentage.setSelected(false);
		     		
					 for (ActivityRecored var : activity_report.Activity_Array) 
					 { 
						 
							 if(var.DateOfReport.equals(selectedDate))
							 {
								ActiveSubtextField.setText(var.numofActiveSubscribers);
								LockedSubtextField.setText(var.numofLockedSubscribers);
								InActiveSubtextField.setText(var.numofSuspendedSubscribers);
								TotalBorrowedtextField.setText(var.numofBorrowedCopies);
								LatenessSubtextField.setText(var.numofLateSubscribers);
							 }  
					 }
		     	 }
		     });
//================================================================	
		     rdbtnPercentage.addActionListener(new ActionListener()
		     {
		     	public void actionPerformed(ActionEvent arg0)
		     	{
		     		rdbtnInNumbers.setSelected(false);
		     		rdbtnPercentage.setSelected(true);
					 for (ActivityRecored var : activity_report.Activity_Array) 
					 { 
						 
							 if(var.DateOfReport.equals(selectedDate))
							 {
								 ActiveSubtextField.setText(String.valueOf(df.format(var.PresentofActiveSubscribers))+"%");
								 LockedSubtextField.setText(String.valueOf(df.format(var.PresentofLockedSubscribers))+"%");
								 InActiveSubtextField.setText(String.valueOf(df.format(var.PresentofInactiveSubscribers))+"%");
								 TotalBorrowedtextField.setText(String.valueOf(df.format(var.PresentofBorrowedCopies))+"%");
								 LatenessSubtextField.setText(String.valueOf(df.format(var.PresentofLateSubscribers))+"%");
							 }  
					 }
		     		
		     	}
		     });
	     
//================================================================		     
			btnProduceReport.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
					activity_report_current = managerMainUI.muip.getActivityReport(2);
					if(activity_report_current.query_res != -1)
					{
						if(activity_report_current.query_res == 1)
						 {			
								 ActiveSubtextField.setText("");
								 LockedSubtextField.setText("");
								 InActiveSubtextField.setText("");
								 TotalBorrowedtextField.setText("");
								 LatenessSubtextField.setText("");
							 for (ActivityRecored var : activity_report_current.Activity_Array) 
							 {
								ActiveSubtextField.setText(var.numofActiveSubscribers);
								LockedSubtextField.setText(var.numofLockedSubscribers);
								InActiveSubtextField.setText(var.numofSuspendedSubscribers);
								TotalBorrowedtextField.setText(var.numofBorrowedCopies);
								LatenessSubtextField.setText(var.numofLateSubscribers);
						    	DatesComboBox.addItem(var.DateOfReport);
							 }
							 
							 showActivity(); 
							 
						 }
						 else
						if(activity_report_current.query_res == 2)	 
							JOptionPane.showMessageDialog( WelcomeUI.wui," report with this today date already exist! ","info",JOptionPane.INFORMATION_MESSAGE);
					}
					else
					 JOptionPane.showMessageDialog( WelcomeUI.wui," there was an error in report ","warning",JOptionPane.WARNING_MESSAGE);
				}
			});
	     
//================================================================		

			DatesComboBox.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					 JComboBox cb = (JComboBox)e.getSource();

					 for (ActivityRecored var : activity_report.Activity_Array) 
					 { 
						 try
						 {
							 if(var.DateOfReport.equals(cb.getSelectedItem()))
							 {
								selectedDate = var.DateOfReport;
								 
								 if(rdbtnInNumbers.isSelected())
								 {
									 ActiveSubtextField.setText(var.numofActiveSubscribers);
									 LockedSubtextField.setText(var.numofLockedSubscribers);
									 InActiveSubtextField.setText(var.numofSuspendedSubscribers);
									 TotalBorrowedtextField.setText(var.numofBorrowedCopies);
									 LatenessSubtextField.setText(var.numofLateSubscribers);
									 
								 }
								 else
								 if(rdbtnPercentage.isSelected())
								 {
									 ActiveSubtextField.setText(String.valueOf(df.format(var.PresentofActiveSubscribers))+"%");
									 LockedSubtextField.setText(String.valueOf(df.format(var.PresentofLockedSubscribers))+"%");
									 InActiveSubtextField.setText(String.valueOf(df.format(var.PresentofInactiveSubscribers))+"%");
									 TotalBorrowedtextField.setText(String.valueOf(df.format(var.PresentofBorrowedCopies))+"%");
									 LatenessSubtextField.setText(String.valueOf(df.format(var.PresentofLateSubscribers))+"%");
 
								 }
							 }  
						 }
						 catch(NullPointerException e1)
						 {
							 
						 }
					 }  
				 }
					  
			 });
			
			
	}//init
	
	public void showActivity()
	{
		 DatesComboBox.removeAllItems();
		 activity_report =  managerMainUI.muip.getActivityReport(1);
		 if(activity_report.query_res!=2)
		 {
		     for (ActivityRecored var : activity_report.Activity_Array)
	         { 
		    	 try
		    	 {
			    	 if(!var.DateOfReport.isEmpty()) 
			    	 {	
			    		 DatesComboBox.addItem(var.DateOfReport);		 
			    	 }
		    	 }
		    	 catch(NullPointerException e)
		    	 {
		    		 e.printStackTrace();
		    	 }
	         }
		 }
		 else
			 JOptionPane.showMessageDialog( WelcomeUI.wui," there are no privous reports in the system ","info",JOptionPane.INFORMATION_MESSAGE); 
	 }
//==============================================================================================	
	public String ConvertFromDateToString(Date date , DateFormat format)
	{
		String str_date = "";
		str_date = format.format(date);
		return str_date;
	}
	public Date ConvertFromStringToDate(String str_date , DateFormat format)
	{
		Date date = null ; 
		
		try {
			 date = format.parse(str_date);
			} 
		catch (ParseException e)
			{
				//e.printStackTrace();
				System.out.println("couldn't convert from string to date , check format");
			}
		
		return date;
	}
	
	public void paintComponent(Graphics g) 
	{
		g.drawImage((OBLSettings.IMG_DEFAULT_BACKGROUND).getImage(), 0, 0, OBLSettings.pane2Size[0], OBLSettings.pane2Size[1],null);
		
	}
}//class

	

